<?php


namespace app\manager\controller;


use app\common\controller\Common;
use app\common\model\Category;
use Redis;
use think\Db;
use think\facade\App;

class Merchant extends Common
{
    public function lists()
    {
        if ($this->request->isAjax()) {
            $limit = $this->getPageSize();
            $reMer = \app\common\model\Merchant::where('status','in',[2,3,4,5])->paginate($limit);
            if (count($reMer) == 0) return $this->errJson('暂无数据');
            return $this->listJson($reMer->items(), $reMer->total());
        } else {
            return view();
        }

    }

    public function approval()

    {//审核列表
        if ($this->request->isAjax()) {
            $limit = $this->getPageSize();
            $reMer = \app\common\model\Merchant::where('status', '=', -1)->paginate($limit);
            if (count($reMer) == 0) return $this->errJson('暂无数据');
            return $this->listJson($reMer->items(), $reMer->total());
        } else {
            return view();
        }
    }

    public function show()
    {
        $merid = request()->param('merid');
        if (is_null($merid)) return $this->errJson('参数错误');
        $reMer = \app\common\model\Merchant::where('merid', '=', $merid)->find();
        $cate = Category::field('cid,title,ico,pid,path,sort,depth')->where('status', '=', 2)->all();
        if (empty($reMer)) return $this->errJson('数据异常');
        if ($reMer) {
            $area = Db::query("select area_id,area_title,citycode FROM area WHERE citycode=471");
            $this->assign('area', $area);
            $this->assign('cate', $cate);
        }
        $this->assign('reMer', $reMer);
        return view();
    }

    public function edit()
    {
        $param = request()->param();

        if (is_null($param['merid'])) return $this->errJson('参数错误');

        $param['updatetime'] = time();
        unset($param['file']);
        Db::startTrans();
        try {
            $ret = \app\common\model\Merchant::where('merid', '=', $param['merid'])->update($param);
            if ($ret) {
                if (!is_null($param['latitude']) && !is_null($param['longitude'])) {
                    $merid = $param['merid'];
                    $redis = new Redis();
                    if ($redis->connect('127.0.0.1', 6379)) {
                        $param = array('geoadd', 'store', $param['longitude'], $param['latitude'], $merid);
                        $ret = call_user_func_array(array($redis, 'rawCommand'), $param);
                        if ($ret==1||$ret==0) Db::commit();
                    } else {
                        Db::rollback();
                        return $this->errJson('更新失败');
                    }
                }
                Db::commit();
                return $this->okJson('更新成功');
            }
        }catch (\Exception $exception){
            Db::rollback();
            return $this->errJson('更新失败');
        }
    }
    /*
     * 文件上传
     */
    public function uploads()
    {
        $merid = input('merid');
        if (is_null($merid)) return $this->errJson('参数错误');
        $ret = $this->upfile('file', [100, 100]);
        if (isset($ret['code']) && $ret['code'] == 2){
            $reMer= \app\common\model\Merchant::where('merid','=',$merid)->find();
            $images =$reMer->title_img;
            if ($images==NULL||$images=="") {
                $images=array();
            }else{
                $images = explode(',',$images);
            }
            if (count($images)<3) {//限制上传三张以内
                array_push($images,$ret['filepath']);
                $reMer->title_img=implode(',',$images);
                $res = $reMer->save();
                if ($res) return $this->okJson('上传成功', $ret);
            }
        }
        return $this->errJson('上传失败');
    }

    //删除文件
    public function delfile()
    {
        $path = input('path');
        $merid = input('merid');
        if (empty($path)) return $this->errJson('路径不能为空');
        if (empty($merid)) return $this->errJson('参数不能为空');
        $dir=App::getRootPath();
        $res = unlink($dir.'public'.$path);
        if ($res){
           $reMer= \app\common\model\Merchant::where('merid','=',$merid)->find();
            $imgs = $reMer->title_img;
            $imgs=explode(',',$imgs);
            $key = array_search($path, $imgs);
            if ($key !== false){
                array_splice($imgs, $key, 1);
                $reMer->title_img=implode(',',$imgs);
                $ret = $reMer->save();
                if ($ret) return $this->okJson('图片删除成功');
            }
        }
        return $this->errJson('图片删除失败');
        //
    }

}